<template>
  <view class="body">
    <view class="goods flex flex-row">
      <view class="imgBox">
        <image class="image" :src="goods.image"></image>
      </view>
      <view class="goodsInfo">
        <view  class="flex flex-row">
          单价
          <view class="price fl1" v-if="goods.isZeroPrice || goods.mobileOrderIsAllowModifyTaxPrice == '0' || goods.giftId || goods.promotionType == 3 ">{{ getPrice(goods.billTaxPrice || goods.taxPrice) }}</view>
          <view class="price fl1 flex" v-else><input v-model="goods.taxPrice" type="number" @change="$event=>upPrice('taxPrice',$event)"/></view>
          箱价
          <view class="price fl1" v-if="goods.isZeroPrice || goods.mobileOrderIsAllowModifyTaxPrice == '0' || goods.giftId || goods.promotionType == 3 ">{{ getPrice(goods.billTaxBoxPrice || goods.taxBoxPrice) }}</view>
            <view class="price fl1 flex" v-else><input v-model="goods.taxBoxPrice" type="number" @change="$event=>upPrice('taxBoxPrice',$event)"/></view>
        </view>
        <view class="name">
          {{ goods.goodsName }}
        </view>
          <view v-if="!goods.giftId && goods.mobileOrderIsAllowShowStock === '1'" class="goodsPrice">库存:{{ `${goods.usableBoxNum}${goods.boxUnitName}${goods.usableMainNum}${goods.mainUnitName}` }}</view>
          <view class="barCode ">
          <view class="fl1">条码 {{ goods.barcode }}</view>
          <view class="fl1">箱条码 {{ goods.boxBarcode }}</view>
        </view>
      </view>
    </view>
    <scroll-view :scroll-top="scrollTop" style="height: 670rpx" scroll-y="true" class="scroll-Y">
      <!-- 品牌 -->
      <view class="box">
        <view class="title" >品牌 </view>
        <view class="content" >
          <view class="promotion promotionClick" v-if="commonGoodsList.length === 0"  @click="setCurrentPromotion(false)">
            <image class="goodsImage" :src="goods.image" />{{ goods.goodsBrandName }}
          </view>
          <view class="flex flex-row flex-warp pd"   v-else>
            <view class="promotion" v-for="(item,index) in commonGoodsList" :key="index" :class="{'promotionClick':goods.goodsId === item.goodsId}"
                  @click="setGoods(item)">
              <image class="goodsImage" :src="item.image" />{{ item.goodsBrandName }}
            </view>
          </view>
        </view>
      </view>
      <!-- 促销 -->
      <view class="box" v-if="goods.giftPromotionInfoList&&type!=='3'">
        <view class="title">促销</view>
        <view class="content flex flex-row wrap pd" >
          <view class="promotion"
                v-for="item in goods.giftPromotionInfoList"
                @click="setCurrentPromotion(item)"
                :class="{'promotionClick':currentPromotionId === item.promotionId }"
          ><image class="goodsImage" :src="goods.image" /><view class="promotionName">{{ item.promotionName }}</view></view>
        </view>
      </view>
      <!-- 数量 -->
      <view class="box">
        <view class="title">数量</view>
        <view class="numBox content flex flex-row">
          <view class="fl1 flex flex-row">
        <span class="numName">
          箱数
        </span>
            <view class="flex flex-row">
              <view class="reduce" @tap="setGoodsNum(goods,'boxNum','reduce')">-</view>
              <input class="ipt" type="number"  @change="computedCount(goods,'boxNum')" v-model="goods.boxNum">
              <view class="add" @tap="setGoodsNum(goods,'boxNum','add')">+</view>
            </view>
          </view>
          <view class="fl1 flex flex-row ">
        <span class="numName">
          零数
        </span>
            <view class="flex flex-row">
              <view class="reduce"  @tap="setGoodsNum(goods,'mainNum','reduce')">-</view>
              <input class="ipt" type="number"  @change="computedCount(goods,'mainNum')" v-model="goods.mainNum">
              <view class="add" @tap="setGoodsNum(goods,'mainNum','add')">+</view>
            </view>
          </view>
        </view>
      </view>
      <!-- 赠品 -->
      <view class="box" v-if="currentPromotionId !== -1&&type!=='3'">
        <view class="title">赠品<span class="giftNum">可赠数量:{{ giveCount }}</span></view>
        <view class="content">
          <view
              v-for="(item,i) in giftList" :key="i"
              class="gift flex flex-column"
          >
            {{ item.goodsName }}
            <view class="flex flex-row">
              <view class="fl1 flex flex-row">
                箱数
                <view class="flex flex-row">
                  <view class="reduce" @tap="setGiftNum(item,'boxNum','reduce')">-</view>
                  <input class="ipt" v-model="item.boxNum" @change="computedCount(item,'boxNum')" type="number">
                  <view class="add" @tap="setGiftNum(item,'boxNum','add')">+</view>
                </view>
              </view>
              <view class="fl1 flex flex-row">
                零数
                <view class="flex flex-row">
                  <view class="reduce"  @tap="setGiftNum(item,'mainNum','reduce')">-</view>
                  <input class="ipt" v-model="item.mainNum" type="number"  @change="computedCount(item,'mainNum')">
                  <view class="add"  @tap="setGiftNum(item,'mainNum','add')">+</view>
                </view>
              </view>
            </view>
          </view>
        </view>
      <!-- </view>
      <view style="height: 100rpx;width: 100%"></view>

    </scroll-view> -->
  </view>
    <view style="height: 100rpx;width: 100%"></view>
  </scroll-view>

  <add-shopping-cart v-show="isAdd&&(type!=='3'||type!=='5')" @add="addCart" :type="type"/>
  <add-shopping-cart v-show="isAdd&&(type==='3'||type==='5')" @submit="appear" :type="type" :news="news"/>
</view>
</template>

<script>
import {basGoodsListForAdd, giftPromotionDetail, paperGoodsWithEmptyBoxGoodsList} from "../service/api/shopping";
import addShoppingCart from "./addShoppingCart";
import {debounce, setImage} from "../service/auth";
export default {
  name: "selectGoods",
  components:{addShoppingCart},
  props:{
    photo:{
      type:String,
      default:''
    },
    type:{
      type:String,
      default:'1'
    },
    news:{
      type:String,
      default:''
    },
    goods:{
      type:Object,
      default: {
        billTaxPrice: 0,
        billTaxBoxPrice: 0,
      }
    },
    //当前选择的促销活动
    currentPromotionId:{
      type:Number,
      default: -1
    },
    //是否显示添加购物车
    isAdd:{
      type:Boolean,
      default: true
    },
    params:{
      type:Object,
      default: ()=>{}
    },
    //用于修改当前赠品
    currentGiftList:{
      type:Array,
      default:()=>[]
    },
    customerId:[String,Number]
  },
  created() {
    const app = getApp()
    this.appletExtraDict = app.globalData.appletExtraDict
  },
  watch:{
    currentGiftList:{
      handler(n){
        if (n.length){
          this.getPromotionList(this.currentPromotionId).then(res=>{
            n.forEach(item=>{
              const index = this.giftList.findIndex(val=>{
                return val.goodsId === item.goodsId
              })
              if(~index)this.giftList[index] = Object.assign({},item)
              this.debounce(this.getGiftCount,500,false)
              this.$forceUpdate()
            })
          })
        }
      },
      deep:true,
      immediate: true,
    },
    'goods.goodsId'(n){
      if (this.oldCommonName !== this.goods.commonName){
        this.commonGoodsList = []
        this.oldCommonName = this.goods.commonName
        if (this.goods.commonName){
          this.getCommon()
        }
      }
    }
  },
  data(){
    return{
      giftList: [],
      appletExtraDict:[],
      promotionGoodsList: [],
      ruleList: [],
      giveCount: 0,
      timeOut:"",
      commonGoodsList:[],
      oldCommonName:'啦啦啦'
    }
  },
  computed:{
    getPrice(){
      return (price)=>{
        if (price) return  price.toFixed(2)
        return price
      }
    }
  },

  methods:{
    setCurrentPromotion(val){
      if (val && this.currentPromotionId !== val.promotionId) {
        this.$emit('update:currentPromotionId',val.promotionId)
        this.getPromotionList(val.promotionId)
      }else {
        this.$emit('update:currentPromotionId', -1)
        this.giftList= []
      }
    },
    upPrice(type){
        if (type === 'taxPrice'){
            this.goods.taxPrice = Number(this.goods.taxPrice)
            this.goods.taxBoxPrice = this.goods.taxPrice * this.goods.boxUnitCount
        }else{
            this.goods.taxBoxPrice = Number(this.goods.taxBoxPrice)
            this.goods.taxPrice = (this.goods.taxBoxPrice / this.goods.boxUnitCount).toFixed(2)
        }
        this.$emit('update:goods',this.goods)
    },
    getCommon(){
      const data = {
        ...this.params,
        customerId:this.customerId,
        commonName:this.goods.commonName
      }
      data.page = 1
      data.size = 999
      basGoodsListForAdd(data).catch(err=>{
        this.commonGoodsList = err.content
        this.commonGoodsList.forEach(item=>{
          this.$set(item,'image',setImage(item.imageUrl))
          this.$set(item,'boxNum',0)
          this.$set(item,'mainNum',0)
        })
      })
    },

    setVal(e){
      console.log(e)
    },
    setGoodsNum(obj,attribute,type){
      if (type === 'add'){
        this.$set(obj,attribute,Number(obj[attribute])+1)
      }else if ('reduce'){
        if(!obj[attribute])return
        this.$set(obj,attribute,Number(obj[attribute])-1)
      }
      this.$emit('update:goods',obj)
      this.computedCount(obj,attribute)
    },
    setGiftNum(obj,attribute,type){
      if (type === 'add'){
        this.$set(obj,attribute,Number(obj[attribute])+1)
      }else if ('reduce'){
        if(!obj[attribute])return
        this.$set(obj,attribute,Number(obj[attribute])-1)
      }
      this.computedCount(obj,attribute)
    },
    appear(){
      if (!this.goods.goodsCount)return uni.showToast({title:'请先选择商品数量',icon:'none'})
      this.$emit('addstock',this.goods)
      // this.close()
    },
    addCart(){
      if (!this.goods.goodsCount)return uni.showToast({title:'请先选择商品数量',icon:'none'})
      if (this.currentPromotionId === -1)return this.$emit('addGoods',this.goods)
      const promotionGoodsList = [this.goods]
      //获取活动商品箱数
      let promotionBoxCount = 0
      //获取赠品数量
      let promotionCount = 0
      promotionGoodsList.forEach(item => {
        promotionCount += Number(item.goodsCount)
        promotionBoxCount += Number(item.boxNum)
      })
      const giftList = this.giftList.filter(item => item.goodsCount > 0)
      //获取赠品箱数
      let giftBoxCount = 0
      //获取赠品数量
      let giftCount = 0
      giftList.forEach(item => {
        giftCount += Number(item.goodsCount)
        giftBoxCount += Number(item.boxNum)
      })
      //判断活动商品满赠类型
      if (this.ruleList[0].reachType === 2) {
        //阶梯过滤出当前促销符合的条件
        const satisfyList = this.ruleList.filter(item => item.full <= promotionCount)
        //查询最大符合条件
        let satisfy = null
        satisfyList.forEach(item => {
          if (!satisfy || item.full > satisfy.full) {
            satisfy = item
          }
        })
        //符合促销
        if (satisfy) {
          //计算最大满足倍数
          const promotionNum = parseInt(promotionCount / satisfy.full)
          //计算最大赠送赠品数量
          const giftNum = promotionNum * satisfy.result
          //判断当前赠品数量是否超最大可赠送数量
          //判断赠品类型
          if (satisfy.resultType === 2) {
            if (giftCount > giftNum) {
              uni.showToast({title:`当前赠品数量大于可赠送数量，不允许下单;可赠数量为${giftNum}瓶，当前赠送数量为${giftCount}瓶`,icon:'none'})
            } else {
              this.submitPromotionList()
            }

          } else if (satisfy.resultType === 3) {
            //判断当前赠品数量是否超最大可赠送数量
            const mainGift = this.giftList.find(item => item.mainNum > 0)
            if (mainGift) {
              uni.showToast({title:'赠送规格为箱,不允许输入零数',icon:'none'})
              return
            }
            if (giftBoxCount > giftNum) {
              uni.showToast({title:`当前赠品数量大于可赠送数量，不允许下单;可赠数量为${giftNum}箱，当前赠送数量为${giftBoxCount}箱`,icon:'none'})
            } else {
              this.submitPromotionList()
            }
          }else {
            const goodsList = this.promotionGoodsList.filter(item=>item.goodsCount > 0)
            goodsList.forEach(item=>{
              this.hideModal = false
              item.remark = item.giftRemark
              this.taxAmount = item.taxPrice * item.goodsCount
              item.joldTaxPrice = item.taxPrice
              item.joldTaxBoxPrice =item.taxBoxPrice
              this.$emit('addGoods',[Object.assign({},item),false])
            })
          }
        }}
      else  if (this.ruleList[0].reachType === 3) {
        //阶梯过滤出当前促销符合的条件
        const satisfyList = this.ruleList.filter(item => item.full <= promotionBoxCount)
        //查询最大符合条件
        let satisfy = null
        satisfyList.forEach(item => {
          if (!satisfy || item.full > satisfy.full) {
            satisfy = item
          }
        })
        //符合促销
        if (satisfy){
          //计算最大满足倍数
          const promotionNum = parseInt(promotionBoxCount / satisfy.full)
          //计算最大赠送赠品数量
          const giftNum = promotionNum * satisfy.result
          console.log(`活动商品箱数${promotionBoxCount},满促条件${ satisfy.full },单词满足可赠为${promotionNum},满足可赠箱数${giftNum}`)
          //判断当前赠品数量是否超最大可赠送数量
          //判断赠品类型
          if (satisfy.resultType === 2) {
            if (giftCount > giftNum) {
              uni.showToast({title:`当前赠品数量大于可赠送数量，不允许下单;可赠数量为${giftNum}瓶，当前赠送数量为${giftCount}瓶`,icon:'none'})
            } else {
              this.submitPromotionList()
            }
          } else if (satisfy.resultType === 3) {
            //判断当前赠品数量是否超最大可赠送数量
            const mainGift = this.giftList.find(item=>item.mainNum > 0)
            if (mainGift){
              uni.showToast({title:'赠送规格为箱,不允许输入零数',icon:'none'})
              return
            }
            if (giftBoxCount > giftNum) {
              uni.showToast({title:`当前赠品数量大于可赠送数量，不允许下单;可赠数量为${giftNum}箱，当前赠送数量为${giftBoxCount}箱`,icon:'none'})
            } else {
              this.submitPromotionList()
            }
          }
        }else {
          // uni.showToast({title: '当前活动商品数量不满足促销条件', icon: 'none'})
          const goodsList = this.promotionGoodsList.filter(item=>item.goodsCount > 0)
          goodsList.forEach(item=>{
            this.hideModal = false
            item.remark = item.giftRemark
            this.taxAmount = item.taxPrice * item.goodsCount
            item.joldTaxPrice = item.taxPrice
            item.joldTaxBoxPrice =item.taxBoxPrice
            this.$emit('addGoods',Object.assign({},item))
          })
        }
      }
    },
    async submitPromotionList(){
      const date = new Date().getTime()
      const  giftList = this.giftList.filter(item => item.goodsCount > 0)
      const giftEmptyBoxList = giftList.filter(item=>item.packageType == 1 && item.isGiftPaperBox == 0)
      const goodsIds = giftEmptyBoxList.map(item=>item.goodsId).join(',')
      // const promotionList = this.promotionGoodsList.filter(item => item.goodsCount > 0)
      const promotionList = [this.goods]
      // const emptyBoxList = []
      if (goodsIds){
        let content
        await paperGoodsWithEmptyBoxGoodsList({ids:goodsIds,customerId: this.customerId}).catch(res=>{
          content = res.content
        })
        for (let i of content){
          const gift = this.giftList.find(item=>item.goodsId === i.id)
          if (!gift){
            continue
          }
          let uuid
          // = uuidv4().split('-')
          uuid = `${new Date().getTime()}${gift.goodsId}`
          const emptyBox = i.paperEmptyBoxGoodsInfo
          if (!emptyBox){
            continue
          }
          emptyBox.boxNum = Number(gift.boxNum)
          emptyBox.mainNum = 0
          emptyBox.goodsCount = emptyBox.boxNum * Number(emptyBox.boxUnitCount)
          emptyBox.taxAmount = emptyBox.goodsCount * emptyBox.taxPrice
          emptyBox.billTaxAmount = emptyBox.billTaxPrice * emptyBox.goodsCount
          emptyBox.boxCount = emptyBox.boxNum
          emptyBox.isEmptyBoxAdd = '1'
          emptyBox.isPromotionAdd = 1
          emptyBox.remark = this.remark
          gift.promotionEmptyBoxKey = uuid
          emptyBox.promotionEmptyBoxKey = uuid
          const index = this.giftList.indexOf(gift)
          giftList.splice(index + 1,0,emptyBox)
          // emptyBoxList.push(emptyBox)
        }
      }
      //多促销商品改单商品
      // const promotionGoodsList = [...this.promotionGoodsList.filter(item => item.goodsCount > 0),...giftList]
      const promotionGoodsList = [this.goods,...giftList]
      for (let i of promotionGoodsList){
        i.promotionGroupKey = date
        i.remark = this.remark
        i.isPromotion = 1
        i.promotionId = i.isEmptyBoxAdd?null:this.promotionId
        i.acceptBoxNum=i.boxNum
        i.isOne =  0
        i.acceptMainNum=i.mainNum
        i.promotionCode = i.isEmptyBoxAdd?null:this.promotionCode
        i.promotionName = i.isEmptyBoxAdd?null:this.promotionName
        i.sourceDetailType = i.isEmptyBoxAdd?'1':'2'
      }
      if (promotionList.length ===1 && giftList.filter(item=>item.isEmptyBoxAdd !== '1').length === 1){
        const promotionGoods = promotionGoodsList[0]

        promotionGoods.promotionData = {}
        promotionGoods.promotionData.gift = promotionGoodsList[1]
        promotionGoods.promotionData.ruleList = this.ruleList
        promotionGoods.isOne = 1
        promotionGoods.promotionData.emptyBox = promotionGoodsList[2]
        promotionGoods.promotionData = JSON.stringify(promotionList[0].promotionData)
      }
      // this.hideModal = false
      this.goods.currentPromotionId = this.currentPromotionId
      this.$emit('subPromotionList',promotionGoodsList)
      this.close()
    },
    close(){
      this.$emit('close')
    },
    //加个防抖
    debounce(fn,time,...arg){
      this.timeOut && clearTimeout(this.timeOut)
      this.timeOut = setTimeout(()=>{
        this.timeOut = null
        this.giveCount = fn(...arg)
      },time)
    },

    async getPromotionList(promotionId){
      try{
        await giftPromotionDetail({customerId:this.customerId,promotionId:promotionId})
      }catch ({content}) {
        this.remark = content.promotionInfo.remark
        this.promotionName = content.promotionInfo.name
        this.ruleList = content.promotionInfo.detailList[0].ruleList
        const giftList = content.promotionInfo.detailList[0].giftList
        this.giftList = giftList.filter(item => item.isGift === 1)
        this.promotionGoodsList = giftList.filter(item => item.isGift === 0)
        for (let i in this.giftList) {
          const goodsTagId = this.giftList[i].goodsTagId
          const goodsTagName = this.giftList[i].goodsTagName
          const isGiftPaperBox = this.giftList[i].isGiftPaperBox
          let goodsImgUrl
          const goods = content.goodsList.find(val => this.giftList[i].goodsId === val.goodsId)
          // try {
          //   if (goods.imageUrl) {
          //     goodsImgUrl = JSON.parse(goods.imageUrl)[0].path
          //   } else {
          //     goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
          //   }
          // } catch (e) {
          //   goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
          // }

          this.$set(this.giftList, i, Object.assign({}, goods,
              {
                boxNum: 0,
                mainNum: 0,
                goodsCount: 0,
                taxAmount: 0,
                noTaxAmount: 0,
                taxPrice: 0,
                noTaxPrice: 0,
                taxBoxPrice: 0,
                noTaxBoxPrice: 0,
                oldTaxPrice: 0,
                oldNoTaxPrice: 0,
                oldTaxBoxPrice: 0,
                oldNoTaxBoxPrice: 0,
                oldTaxAmount: 0,
                oldNoTaxAmount: 0,
                oldTax: 0,
                acceptTaxAmount: 0,
                acceptNoTaxAmount: 0,
                acceptTax: 0,
                billTaxPrice: 0,
                billTaxBoxPrice: 0,
                billTaxAmount: 0,
                goodsTagId,
                goodsTagName,
                isGiftPaperBox,
                isZeroPrice: 1,
                isGift: '1',
                promotionType: '3',
                isPromotion: 1,
                image: goodsImgUrl,
                isPromotionAdd: 1,
                promotionId: this.promotionId,
                promotionCode: this.promotionCode,
                promotionName: this.promotionName,
              }
              )
          )

        }
        // for(let i in this.promotionGoodsList){
        //   let goodsImgUrl
        //   const goods = content.goodsList.find(val=>this.promotionGoodsList[i].goodsId === val.goodsId)
        //   try {
        //     if (goods.imageUrl){
        //       goodsImgUrl = JSON.parse(goods.imageUrl)[0].path
        //     }else {
        //       goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
        //     }
        //   }catch (e) {
        //     goodsImgUrl = 'https://www.zjjqf.cn/file/static/undefined.png'
        //   }
        //
        //   this.$set(this.promotionGoodsList, i, Object.assign({}, content.goodsList.find(val => this.promotionGoodsList[i].goodsId === val.goodsId), {
        //     boxNum: 0, mainNum: 0, goodsCount: 0,
        //     isRequired: this.promotionGoodsList[i].isRequired,
        //     isPromotionAdd: '1',
        //     promotionId: this.promotionId,
        //     promotionCode: this.promotionCode,
        //     promotionName: this.promotionName,
        //     isGift: '0',
        //     image: goodsImgUrl
        //   }))
        // }
        // const goods = this.promotionGoodsList.find(item=>item.goodsId === this.goods.goodsId)
        // goods.giftPromotionInfoList = this.goods.giftPromotionInfoList
        const goods = Object.assign(this.goods, {
          isPromotionAdd: '1',
          promotionId: this.promotionId,
          promotionCode: this.promotionCode,
          promotionName: this.promotionName,
          isGift: '0',
        })
        this.$emit('update:goods', goods)
        this.isZeroPrice = this.goods.isZeroPrice
        return Promise.resolve(true)
      }
    },
    computedCount(row, type, index) {
      row[type] = Number(row[type])
      if (type === 'boxNum') {
        row.goodsCount = Number(row.boxNum) * Number(row.boxUnitCount) + Number(row.mainNum)
        if (row.isGift == '1') {
          row.taxBoxPrice = 0
          row.noTaxPrice = 0
          row.noTaxBoxPrice = 0
          row.taxAmount = 0
          row.noTaxAmount = 0
          row.tax = 0
        } else {
          row.taxAmount = row.goodsCount * Number(row.taxPrice)
          row.noTaxAmount = row.goodsCount * Number(row.taxPrice)
        }
      } else if (type === 'mainNum') {
        if (Number(row.mainNum) >= Number(row.boxUnitCount)) {
          const boxCount = parseInt(Number(row.mainNum) / Number(row.boxUnitCount))
          const mainCount = Number(row.mainNum) % Number(row.boxUnitCount)
          row.boxNum += boxCount
          row.mainNum = mainCount
        }
        row.goodsCount = Number(row.boxNum) * Number(row.boxUnitCount) + Number(row.mainNum)
        if (row.isGift == '1') {
          row.taxBoxPrice = 0
          row.noTaxPrice = 0
          row.noTaxBoxPrice = 0
          row.taxAmount = 0
          row.noTaxAmount = 0
          row.tax = 0
        } else {
          row.taxAmount = row.goodsCount * Number(row.taxPrice)
          row.noTaxAmount = row.goodsCount * Number(row.taxPrice)
        }
      }
      row.billTaxAmount = row.goodsCount * Number(row.billTaxPrice)
      row.boxCount = Number(row.goodsCount) / Number(row.boxUnitCount).toFixed(6)
      if(this.currentPromotionId !== -1)this.debounce(this.getGiftCount,500,row.isGift == 1)
      this.$forceUpdate()
    },
    setGoods(goods){
      this.$emit('update:goods',goods)
    },
    getGiftCount(isGift) {
      if (!this.promotionGoodsList) {
        return
      }
      const promotionGoodsList = this.promotionGoodsList.filter(item => item.goodsCount > 0)
      // 获取活动商品箱数
      let promotionBoxCount = 0
      // 获取赠品数量
      let promotionCount = 0
      promotionCount = Number(this.goods.goodsCount)

      promotionBoxCount = Number(this.goods.boxNum)      //多对多促销
      // promotionGoodsList.forEach(item => {
      //   promotionCount += Number(item.goodsCount)
      //   promotionBoxCount += Number(item.boxNum)
      // })
      const giftList = this.giftList.filter(item => item.goodsCount > 0)
      // 获取赠品箱数
      let giftBoxCount = 0
      // 获取赠品数量
      let giftCount = 0
      giftList.forEach(item => {
        giftCount += Number(item.goodsCount)
        giftBoxCount += Number(item.boxNum)
      })
      // 判断活动商品满赠类型
      if (this.ruleList[0].reachType === 2) {
        // 阶梯过滤出当前促销符合的条件
        const satisfyList = this.ruleList.filter(item => item.full <= promotionCount)
        // 查询最大符合条件
        let satisfy = null
        satisfyList.forEach(item => {
          if (!satisfy || item.full > satisfy.full) {
            satisfy = item
          }
        })
        // 符合促销
        if (satisfy) {
          // 计算最大满足倍数
          const promotionNum = parseInt(promotionCount / satisfy.full)
          // 计算最大赠送赠品数量
          const giftNum = promotionNum * satisfy.result
          // 判断当前赠品数量是否超最大可赠送数量
          if (this.giftList.length === 1 && !isGift) {
            if (satisfy.resultType === 2) {
              this.giftList[0].goodsCount = giftNum
              const boxCount = parseInt(Number(this.giftList[0].goodsCount) / Number(this.giftList[0].boxUnitCount))
              this.giftList[0].mainNum = Number(this.giftList[0].goodsCount) % Number(this.giftList[0].goodsCount.boxUnitCount)
              this.giftList[0].boxNum = boxCount
            } else {
              this.giftList[0].goodsCount = giftNum * this.giftList[0].boxUnitCount
              this.giftList[0].mainNum = 0
              this.giftList[0].boxNum = giftNum
            }
            return `0${satisfy.resultType === 2 ? '瓶' : '箱'}`
          }
          return `${giftNum - (satisfy.resultType === 2 ? giftCount : giftBoxCount)}${satisfy.resultType === 2 ? '瓶' : '箱'}`
        } else {
          return '0瓶'
        }
      } else if (this.ruleList[0].reachType === 3) {
        // 阶梯过滤出当前促销符合的条件
        const satisfyList = this.ruleList.filter(item => item.full <= promotionBoxCount)
        // 查询最大符合条件
        let satisfy = null
        satisfyList.forEach(item => {
          if (!satisfy || item.full > satisfy.full) {
            satisfy = item
          }
        })
        // 符合促销
        if (satisfy) {
          // 计算最大满足倍数
          const promotionNum = parseInt(promotionBoxCount / satisfy.full)
          // 计算最大赠送赠品数量
          const giftNum = promotionNum * satisfy.result
          console.log(`活动商品箱数${promotionBoxCount},满促条件${satisfy.full},单次满足可赠为${promotionNum},满足可赠箱数${giftNum}`)
          // 判断当前赠品数量是否超最大可赠送数量
          // 判断赠品类型
          if (this.giftList.length === 1 && !isGift) {
            if (satisfy.resultType === 2) {
              this.giftList[0].goodsCount = giftNum
              const boxCount = parseInt(Number(this.giftList[0].goodsCount) / Number(this.giftList[0].boxUnitCount))
              this.giftList[0].mainNum = Number(this.giftList[0].goodsCount) % Number(this.giftList[0].goodsCount.boxUnitCount)
              this.giftList[0].boxNum = boxCount
            } else {
              this.giftList[0].goodsCount = giftNum * this.giftList[0].boxUnitCount
              this.giftList[0].mainNum = 0
              this.giftList[0].boxNum = giftNum
            }
            return `0${satisfy.resultType === 2 ? '瓶' : '箱'}`
          }
          return `${giftNum - (satisfy.resultType === 2 ? giftCount : giftBoxCount)}${satisfy.resultType === 2 ? '瓶' : '箱'}`
        } else {
          return '0瓶'
        }
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.body{
  height: 1000rpx;
  width: 750rpx;
  overflow-y: auto;
  border-radius: 30rpx 30rpx 0 0;
  background: #F5F5F5;
  font-family: PingFang SC;
  .goods{
    background: #ffffff;
    padding: 30rpx;
    .imgBox{
      margin-right: 30rpx;
      .image{
        width: 250rpx;
        height: 250rpx;
      }
    }

    .goodsInfo{
      width: 100%;
      .price{
        &:before{
          content: "￥";
          font-size: 30rpx;
        }
        color: #4CABA3;
        font-size: 35rpx;

      }
      .name{
        padding-right: 30rpx;
        font-size: 32rpx;
        min-height: 150rpx;
      }
      .barCode{

      }
    }
  }
}
.box{
  margin: 2%;
  width: 96%;
  background: #ffffff;
  border-radius: 10rpx;
  padding: 20rpx;
  .numBox{
    margin-left: 30rpx;
    .numName{
      line-height: 52rpx;
    }

  }

  .title{
    font-size: 40rpx;
    font-weight: bolder;
    margin-bottom: 30rpx;
    .giftNum{
      margin-left: 50rpx;
      color: red;
      font-size: 30rpx;
    }
  }
  .promotion{
    display: flex;

    align-items: center;
    width: 292rpx;
    height: 120rpx;
    border: #A7A7A7 1px solid;
    border-radius: 15rpx;
    margin-bottom: 50rpx;
  }
  .promotionClick{
    border: #FF222A 1px solid;
    background: #FFF8F8;
    color:red
  }
}
%bdBtn{
  display: flex;
  justify-content: center;
  align-items: center;
  //font-size: 70rpx;
  font-size: 50rpx;
  border-radius: 10rpx;
  width: 60rpx;
  height: 60rpx;
  font-family: sans-serif;
  border: #E3E3E4 1px solid;
}
.reduce{
  @extend %bdBtn;
  color: #E3E3E4;
}
.add{
  @extend %bdBtn;
  color: #ffffff;
  background: #E3E3E4;
}
.ipt{
  width: 70rpx;
  text-align: center;
}
.gift{
  width: 100%;
  font-size: 32rpx;
  margin-bottom: 30rpx;
}
.goodsImage{
  width: 80rpx;
  height: 80rpx;
  margin: 10rpx
}
.promotionName{
  width: 60%;
  display: flex;
  align-items: center;
  height: 100%;
}
.pd{
  padding: 20rpx;
  justify-content: space-between
}


</style>
